﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GolfLeague.EditModels;
using GolfLeague.ViewModels;

namespace GolfLeague.Models
{
    public class ScheduleInterface
    {
        GolfLeagueEntities golfDB = new GolfLeagueEntities();
        
        public IQueryable<Schedule> GetAllSchedules()
        {
            return from scheduleEntity in golfDB.Schedules
                   orderby scheduleEntity.ScheduleYear descending
                   select scheduleEntity;
        }

        public Schedule GetSchedule(int scheduleID)
        {
            var scheduleQuery = from scheduleEntity in golfDB.Schedules
                                where scheduleEntity.ScheduleID.Equals(scheduleID)
                                select scheduleEntity;

            return scheduleQuery.FirstOrDefault();
        }

        public string GetScheduleName(int scheduledID)
        {
            var scheduleQuery = from scheduleEntity in golfDB.Schedules
                                where scheduleEntity.ScheduleID.Equals(scheduledID)
                                select scheduleEntity;

            return scheduleQuery.FirstOrDefault().ScheduleName;
        }

        public void AddSchedule(Schedule scheduleList)
        {
            golfDB.Schedules.AddObject(scheduleList);
        }

        public void DeleteSchedule(Schedule scheduleList)
        {
            golfDB.Schedules.DeleteObject(scheduleList);
        }

        public void Save()
        {
            golfDB.SaveChanges();
        }

        #region Edit/View Model Builders

        public SchedulesEditModel GetSchedulesEditModel()
        {
            HandicapInterface handicapInterface = new HandicapInterface();
            ScheduleInterface scheduleInterface = new ScheduleInterface();

            List<StandardDropdown> handicapDropdown = new List<StandardDropdown>();
            foreach (HandicapCalculation handicapCalculation in handicapInterface.GetAllHandicapCalculations())
                handicapDropdown.Add(new StandardDropdown
                {
                    dropdownID = handicapCalculation.HandicapCalculationID,
                    dropdownText = handicapCalculation.HandicapName
                });

            return new SchedulesEditModel
            {
                HandicapDropdown = handicapDropdown,
                Schedules = scheduleInterface.GetAllSchedules(),
                NewSchedule = new Schedule()
            };
        }

        public SchedulesEditModel GetSchedulesEditModel(int scheduleID)
        {
            SchedulesEditModel schedulesEditModel = GetSchedulesEditModel();
            schedulesEditModel.NewSchedule = GetSchedule(scheduleID);
            return schedulesEditModel;
        }

        public ScheduleViewModel GetScheduleViewModel(int? id)
        {
            MatchInterface matchInterface = new MatchInterface();
            RoundInterface roundInterface = new RoundInterface();
            IEnumerable<Schedule> schedules = GetAllSchedules();
            List<RoundViewModel> roundViewModel = new List<RoundViewModel>();
            int CurrentScheduleID = (id != null ? Convert.ToInt32(id) : schedules.FirstOrDefault().ScheduleID);
            int roundCounter = 0;
            int activeRound = 0;

            foreach (Round round in roundInterface.GetAllScheduleRounds(CurrentScheduleID))
            {
                if (round.RoundDate.Date < DateTime.Now.Date)
                    roundCounter++;
                else
                    activeRound = roundCounter;

                List<MatchViewModel> matchViewModel = new List<MatchViewModel>();

                foreach (Match match in matchInterface.GetRoundMatches(round.RoundID))
                {
                    IEnumerable<Team> teams = matchInterface.GetMatchTeams(match.MatchID);
                    matchViewModel.Add(new MatchViewModel
                    {
                        MatchTime = match.MatchTime,
                        Team1 = teams.First(),
                        Team2 = teams.Last()
                    });
                }

                roundViewModel.Add(new RoundViewModel
                {
                    Round = round,
                    Matches = matchViewModel
                });
            }

            return new ScheduleViewModel
            {
                Schedules = schedules,
                CurrentScheduleID = CurrentScheduleID,
                ActiveRound = (activeRound != 0 ? activeRound : roundCounter),
                Rounds = roundViewModel
            };
        }

        #endregion
    }
}